<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="logo">
        <img src="../assets/logo.png" alt="logo" class="logo-img">
        <span class="title">智慧教学</span>
      </div>
      <div class="header-right">
        <el-dropdown>
          <span class="user-info">
            <el-avatar :size="32" :src="userAvatar"></el-avatar>
            <span class="username">{{ username }}</span>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <i class="el-icon-office-building"></i>
              学校：{{ userInfo.school || '未设置' }}
            </el-dropdown-item>
            <el-dropdown-item>
              <i class="el-icon-school"></i>
              院系：{{ userInfo.department || '未设置' }}
            </el-dropdown-item>
            <el-dropdown-item>
              <i class="el-icon-reading"></i>
              专业：{{ userInfo.profession || '未设置' }}
            </el-dropdown-item>
            <el-dropdown-item divided @click.native="handleLogout">
              <i class="el-icon-switch-button"></i>
              退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>

    <el-container class="main-container">
      <!-- 侧边栏 -->
      <el-aside width="200px" class="aside">
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          background-color="#2c8a6c"
          text-color="#fff"
          active-text-color="#fff"
          router>
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <el-menu-item index="/classroom">
            <i class="el-icon-reading"></i>
            <span slot="title">我的班级</span>
          </el-menu-item>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-s-check"></i>
              <span>签到管理</span>
            </template>
            <el-menu-item index="/attendance/qrcode">
              <i class="el-icon-camera"></i>
              <span slot="title">二维码签到</span>
            </el-menu-item>
            <el-menu-item index="/attendance/location">
              <i class="el-icon-location"></i>
              <span slot="title">位置签到</span>
            </el-menu-item>
            <el-menu-item index="/attendance/gesture">
              <i class="el-icon-mouse"></i>
              <span slot="title">手势签到</span>
            </el-menu-item>
            <el-menu-item index="/attendance/code">
              <i class="el-icon-key"></i>
              <span slot="title">签到码签到</span>
            </el-menu-item>
            <el-menu-item index="/attendance/quick">
              <i class="el-icon-magic-stick"></i>
              <span slot="title">一键签到</span>
            </el-menu-item>
            <el-menu-item index="/attendance/tasks">
              <i class="el-icon-document"></i>
              <span slot="title">签到任务列表</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Layout',
  data() {
    return {
      username: '',
      userAvatar: '',
      activeMenu: '/home',
      userInfo: {
        name: '',
        school: '',
        department: '',
        profession: ''
      }
    }
  },
  created() {
    // 从localStorage获取用户信息
    const userInfo = localStorage.getItem('userInfo')
    if (userInfo) {
      try {
        const info = JSON.parse(userInfo)
        this.userInfo = info
        this.username = info.name
        console.log('用户信息:', info)
      } catch (e) {
        console.error('解析用户信息失败:', e)
        this.username = '未登录'
      }
    } else {
      this.username = '未登录'
    }
  },
  methods: {
    handleLogout() {
      this.$confirm('确认退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        this.$router.push('/login')
        this.$message.success('已退出登录')
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e8e8e8;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,.08);
}

.logo {
  display: flex;
  align-items: center;
}

.logo-img {
  height: 36px;
  margin-right: 10px;
}

.title {
  font-size: 20px;
  font-weight: bold;
  color: #2c8a6c;
  letter-spacing: 1px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 8px;
  height: 40px;
  border-radius: 20px;
  transition: all 0.3s;
}

.user-info:hover {
  background: #f5f5f5;
}

.username {
  margin-left: 8px;
  color: #333;
  font-size: 14px;
}

.main-container {
  flex: 1;
  overflow: hidden;
}

.aside {
  background-color: #2c8a6c;
  box-shadow: 2px 0 6px rgba(0,21,41,.05);
}

.el-menu-vertical {
  border-right: none;
}

.el-menu-vertical .el-menu-item {
  height: 50px;
  line-height: 50px;
}

.el-menu-vertical .el-menu-item:hover,
.el-menu-vertical .el-submenu__title:hover {
  background-color: #267a5f !important;
}

.el-menu-vertical .el-menu-item.is-active {
  background-color: #1f6b52 !important;
}

.el-menu-vertical .el-menu-item i {
  color: #fff;
}

.main {
  background-color: #f5f7f9;
  padding: 20px;
  overflow-y: auto;
}

.el-dropdown-menu__item i {
  margin-right: 8px;
}
</style>
